package com.atguigu.tingshu.user.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;


    @GetMapping("/wxLogin/{code}")
    public Result<Map<String, String>> wxLogin(@PathVariable String code) {
        Map<String, String> tokenMap = userInfoService.wxLogin(code);
        return Result.ok(tokenMap);
    }

    @GetMapping("/getUserInfo")
    public Result<UserInfoVo> getUserInfo(@RequestHeader String token) {
        return Result.ok(userInfoService.getUserInfo(token));
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo,@RequestHeader String token){
        Long userId = AuthContextHolder.getUserId();
        userInfoService.updateUser(userInfoVo,userId,token);
        return Result.ok();
    }
}
